package com.lwj.liwanjia.view.activity.personal.qian_bao;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.PwdBean;
import com.lwj.liwanjia.bean.TixianBean;
import com.lwj.liwanjia.bean.TixianBean1;
import com.lwj.liwanjia.impl.IsSetPwdView;
import com.lwj.liwanjia.impl.TixianView;
import com.lwj.liwanjia.impl.TixianView1;
import com.lwj.liwanjia.presenter.IsSetPwdPresenter;
import com.lwj.liwanjia.presenter.SubmitTixianPresenter;
import com.lwj.liwanjia.presenter.TixianPresenter;
import com.lwj.liwanjia.utils.PwdDialog;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.ZfPwdDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;


/**
 * Created by 001 on 2017/9/21.
 */

public class YuErTiKuanActivity extends BaseActivity implements TixianView, TixianView1, IsSetPwdView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_zhifubao_name)
    EditText etZhifubaoName;
    @BindView(R.id.et_zhifubao_account)
    EditText etZhifubaoAccount;
    @BindView(R.id.et_zhifubao_money)
    EditText etZhifubaoMoney;
    @BindView(R.id.tv_yuEr1)
    TextView tvYuEr1;
    @BindView(R.id.zhifubao_ll)
    LinearLayout zhifubaoLl;
    @BindView(R.id.et_kaihuhang)
    EditText etKaihuhang;
    @BindView(R.id.et_yinhangka_name)
    EditText etYinhangkaName;
    @BindView(R.id.et_kahao)
    EditText etKahao;
    @BindView(R.id.et_yinhangka_money)
    EditText etYinhangkaMoney;
    @BindView(R.id.tv_yuEr2)
    TextView tvYuEr2;
    @BindView(R.id.bt_sure1)
    Button btSure1;
    @BindView(R.id.bt_sure2)
    Button btSure2;
    @BindView(R.id.yinhangka_ll)
    LinearLayout yinhangkaLl;
    @BindView(R.id.iv_yinhangkaQuan)
    ImageView ivYinhangka;
    @BindView(R.id.iv_zhiFuBaoQuan)
    ImageView ivZhifubao;
    @BindView(R.id.tv_chaochu)
    TextView tvChaochu;
    @BindView(R.id.tv_chaochu2)
    TextView tvChaochu2;


    private boolean zhifuFlag = false;//是否展开
    private boolean yhkFlag = false;//是否展开
    private TixianPresenter tixianPresenter;
    private SubmitTixianPresenter submitTixianPresenter;
    private Map<String, Object> map = new HashMap<>();
    private double money;
    private boolean state;
    private IsSetPwdPresenter isSetPwdPresenter;//判断是否设置了一卡通支付密码
    private PwdDialog pwdDialog;
    private String clickType;//判断点击的是那种提现
    private String zfbName;
    private String txMoney;
    private String zfbAccount;
    private String kaiHuHang;
    private String yhkName;
    private String kaHao;
    private String yhkMoney;
    private double etMoney;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("余额提现");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("提现记录");
        tixianPresenter = new TixianPresenter(this);
        submitTixianPresenter = new SubmitTixianPresenter(this);
        isSetPwdPresenter = new IsSetPwdPresenter(this);
        listener();
    }

    private void listener() {
        etZhifubaoMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString())) {
                    double txMoney = Double.parseDouble(s.toString());
                    if (txMoney > money) {
                        tvChaochu.setVisibility(View.VISIBLE);
                    } else {
                        tvChaochu.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etYinhangkaMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s.toString())) {
                    double txMoney = Double.parseDouble(s.toString());
                    if (txMoney > money) {
                        tvChaochu2.setVisibility(View.VISIBLE);
                    } else {
                        tvChaochu2.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_yu_er_ti_kuan;
    }

    @OnClick({R.id.tv_left, R.id.tv_right, R.id.zhifubao_rl, R.id.bt_sure1, R.id.yinhangka_rl, R.id.bt_sure2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                Intent intent = new Intent(YuErTiKuanActivity.this, RecordActivity.class);
                startActivity(intent);
                break;
            case R.id.zhifubao_rl://点击支付宝提现
                if (state) {
                    Toast.makeText(this, "提现正在申请中，请查看记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (zhifuFlag) {
                    zhifubaoLl.setVisibility(View.GONE);
                    ivZhifubao.setImageResource(R.mipmap.right);
                    zhifuFlag = false;
                } else {
                    zhifubaoLl.setVisibility(View.VISIBLE);
                    ivZhifubao.setImageResource(R.mipmap.tixian_xiala);
                    zhifuFlag = true;
                }
                break;
            case R.id.bt_sure1:
                clickType = "支付宝";
                zfbName = etZhifubaoName.getText().toString().trim();
                zfbAccount = etZhifubaoAccount.getText().toString().trim();
                txMoney = etZhifubaoMoney.getText().toString().trim();
                if (!TextUtils.isEmpty(zfbName) && !TextUtils.isEmpty(zfbAccount) && !TextUtils.isEmpty(txMoney)) {
                    etMoney = Double.parseDouble(txMoney);
                    if (etMoney > money) {
                        Toast.makeText(this, "金额已超过可提现金额", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    isSetPwdPresenter.isSetPwd(SpfUtils.getSpfUtils(this).getToken());
                } else {
                    Toast.makeText(this, "支付宝提现数据不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.yinhangka_rl://点击银行卡提现
                if (state) {
                    Toast.makeText(this, "提现正在申请中，请查看记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (yhkFlag) {
                    yinhangkaLl.setVisibility(View.GONE);
                    ivYinhangka.setImageResource(R.mipmap.right);
                    yhkFlag = false;
                } else {
                    yinhangkaLl.setVisibility(View.VISIBLE);
                    ivYinhangka.setImageResource(R.mipmap.tixian_xiala);
                    yhkFlag = true;
                }
                break;
            case R.id.bt_sure2:
                clickType = "银行卡";
                kaiHuHang = etKaihuhang.getText().toString().trim();
                yhkName = etYinhangkaName.getText().toString().trim();
                kaHao = etKahao.getText().toString().trim();
                yhkMoney = etYinhangkaMoney.getText().toString().trim();
                if (!TextUtils.isEmpty(kaiHuHang) && !TextUtils.isEmpty(yhkName) && !TextUtils.isEmpty(kaHao) && !TextUtils.isEmpty(yhkMoney)) {
                   etMoney = Double.parseDouble(yhkMoney);
                    if (etMoney > money) {
                        Toast.makeText(this, "金额已超过可提现金额", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    isSetPwdPresenter.isSetPwd(SpfUtils.getSpfUtils(this).getToken());
                } else {
                    Toast.makeText(this, "银行卡提现数据不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void isSetPwd(PwdBean pwdBean) {
        if (pwdBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (pwdBean.getState() == 1) {
            if (pwdBean.isData()) {//设置过支付密码
                pwdDialog = new PwdDialog(this);
                pwdDialog.show();
                pwdDialog.setOnFinishInput(new PwdDialog.OnPasswordInputFinish() {
                    @Override
                    public void inputFinish() {
                        map.put("pwd", pwdDialog.getStrPassword());
                        if (clickType.equals("支付宝")) {
                            getMap(zfbAccount, "支付宝", zfbName, etMoney);
                        }else if(clickType.equals("银行卡")){
                            getMap(kaHao, kaiHuHang, yhkName, etMoney);
                        }
                    }
                });
            } else {
                ZfPwdDialog zfPwdDialog = new ZfPwdDialog(this);
                zfPwdDialog.show();
            }
        }
    }

    @Override
    public void submitTixian(TixianBean1 tixianBean1) {
        if (tixianBean1.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (tixianBean1.getState() == 1) {
            if (tixianBean1.getData() == 1) {
                Toast.makeText(this, "提现申请成功", Toast.LENGTH_SHORT).show();
                pwdDialog.dismiss();
                tixianPresenter.getTxState(SpfUtils.getSpfUtils(this).getToken());
            }else if(tixianBean1.getData()==2){//密码错误
                TextView[] tvList = pwdDialog.getTvList();
                for (TextView tv : tvList) {
                    tv.setText("");
                }
                pwdDialog.getTvError().setVisibility(View.VISIBLE);
            }
        }
    }

    //提现状态
    @Override
    public void getTxState(TixianBean tixianBean) {
        if (tixianBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (tixianBean.getState() == 1) {
            money = tixianBean.getData().getMoney();
            state = tixianBean.getData().isState();
            if (!state) {
                btSure1.setBackgroundResource(R.drawable.shape_logo);
                btSure2.setBackgroundResource(R.drawable.shape_logo);
            }
//            else {
//                btSure1.setBackgroundResource(R.drawable.shape_logo1);
//                btSure2.setBackgroundResource(R.drawable.shape_logo1);
//            }
            yinhangkaLl.setVisibility(View.GONE);
            ivYinhangka.setImageResource(R.mipmap.right);
            zhifubaoLl.setVisibility(View.GONE);
            ivZhifubao.setImageResource(R.mipmap.right);
            tvYuEr1.setText("账户余额¥" + money + "元");
            tvYuEr2.setText("账户余额¥" + money + "元");
        }
    }

    //申请提现
    public void getMap(String account, String type, String name, double money) {
        map.put("account", account);
        map.put("type", type);
        map.put("name", name);
        map.put("money", money);
        map.put("token", SpfUtils.getSpfUtils(this).getToken());
        submitTixianPresenter.submitTixian(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tixianPresenter != null) {
            tixianPresenter = null;
        }
        if (submitTixianPresenter != null) {
            submitTixianPresenter = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tixianPresenter.getTxState(SpfUtils.getSpfUtils(this).getToken());
    }
}
